//
//  LCYNetWorkRequestManager.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "LCYNetWorkRequestManager.h"

@interface LCYNetWorkRequestManager ()
///存储回调block的属性
//@property (nonatomic,copy) ResultBlock block;
@end

@implementation LCYNetWorkRequestManager
/// 单例
+ (instancetype)defaultManager {
    static LCYNetWorkRequestManager *manager = nil;
    @synchronized(self) {
        if (!manager) {
            manager = [[LCYNetWorkRequestManager alloc]init];
        }
    }
    return manager;
}

///网络请求方法
+ (void)requestURL:(NSURL *)url RequestMethod:(URLRequestMethod)method Params:(NSDictionary *)param Result:(ResultBlock)resultBlock {
    // 空的可变字符串
    NSMutableString *urlString = [NSMutableString new];
    if ([url isKindOfClass:[NSURL class]]) {
        // 拼接url
        [urlString appendString:url.absoluteString];
        
    } else if ([url isKindOfClass:[NSString class]]) {
        [urlString appendString:(NSString *)url];
    }
    if (urlString.length == 0) {
        NSError *error = [NSError errorWithDomain:@"EPA" code:251 userInfo:@{@"reson":@"你输入的网址不正确"}];
        resultBlock(nil,nil,error);
        return;
    }
    // 把参数字典转换为字符串
    LCYNetWorkRequestManager *manager = [LCYNetWorkRequestManager new];
//    manager.block = resultBlock;
    NSString *paraStr = [manager strFromDic:param];
    
    switch (method) {
        case LCY_GET:{
            if (paraStr.length > 0) {
                //拼接成完整的URL网址
                [urlString appendFormat:@"?%@",paraStr];
                
            }
            [manager requestURL:[NSURL URLWithString:urlString] resultBlock:resultBlock];
        }
            break;
        case LCY_POST:
            [manager postURL:urlString ParaStr:paraStr resultBlock:resultBlock];
            break;
            
        default:
            break;
    }
    
}

///GET网络请求
- (void)requestURL:(NSURL *)url resultBlock:(ResultBlock)block{
    
//    __weak typeof(self) weakSelf = self;
    
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:block];
    
    // 开始网络请求
    [dataTask resume];
}
///POST网络请求
- (void)postURL:(NSString *)urlStr ParaStr:(NSString *)paraStr resultBlock:(ResultBlock)block {
    
    // 创建NSURL
    NSURL *url = [NSURL URLWithString:urlStr];
    
    //创建可变请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    
    [mRequest setHTTPMethod:@"POST"];
    //请求体
    [mRequest setHTTPBody:[paraStr dataUsingEncoding:NSUTF8StringEncoding]];
//    __weak typeof(self) weakSelf = self;
    
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mRequest completionHandler:block];
    
    [dataTask resume];
}

- (NSString *)strFromDic:(NSDictionary *)dic {
    // 空数组
    NSMutableArray *array = [NSMutableArray new];
    
    for (NSString *key in dic) {
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,dic[key]];
        [array addObject:str];
    }
    // 数组拼接字符串
    NSString *para = [array componentsJoinedByString:@"&"];
    NSLog(@"%@",para);
    
    // 返回
    return para;
}

@end
